package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**********************
 *
 *@Author 闫星忠
 *@Time 2021/12/15  11:03
 *
 * 物资分类管理
 *
 */
@Api(tags = "业务模块-物资类别相关接口")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {


    @Autowired
    private ProductCategoryService productCategoryService;


    @ApiOperation(value = "分页展示物资类别列表以及分类树")
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> getCategoryList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                 @RequestParam(value = "pageSize") Integer pageSize) {
        PageVO<ProductCategoryTreeNodeVO> categoryTreeNodeList = productCategoryService.findDepartmentList(pageNum, pageSize);
        return ResponseBean.success(categoryTreeNodeList);
    }



    @ApiOperation(value = "添加页，物资父级分类数据回显")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> pctvList = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(pctvList);

    }


    @ApiOperation(value = "添加物资分类")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategory productCategory){
        productCategoryService.add(productCategory);

        return ResponseBean.success();
    }


    @ApiOperation(value = "编辑页，数据回显")
    @GetMapping("/edit/{id}")
    public ResponseBean<ProductCategoryVO> edit(@PathVariable Long id){
        ProductCategoryVO productCategoryVO = productCategoryService.edit(id);
        return ResponseBean.success(productCategoryVO);
    }

    @ApiOperation(value = "编辑页，数据提交，修改物资分类")
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id ,@RequestBody ProductCategory productCategory){
        productCategoryService.update(id,productCategory);
        return ResponseBean.success();
    }



    @ApiOperation(value = "删除物资分类")
    @DeleteMapping("/delete/{id}")
    public ResponseBean update(@PathVariable Long id){
        try {
            productCategoryService.delete(id);
        } catch (BusinessException e) {
            e.printStackTrace();
            return ResponseBean.error(e);
        }
        return ResponseBean.success();
    }






}
